package com.example.demo.algorithm.lesson.basic.domain;

/**
 * @author liweizhi
 * @date 2020/4/24
 */
public class LinkedListHelper {
    public static Node generateRandomSinglyLinkedList(int size, int maxValue) {
        size = (int) (Math.random() * (size + 1));
        if (size == 0) {
            return null;
        }
        size--;
        Node head = new Node((int) (Math.random() * (maxValue + 1)));
        Node pre = head;
        while (size > 0) {
            size--;
            Node cur = new Node((int) (Math.random() * (maxValue + 1)));
            pre.setNext(cur);
            pre = cur;
        }
        return head;
    }

    public static DoubleNode generateRandomDoubleLinkedList(int size, int maxValue) {
        size = (int) (Math.random() * (size + 1));
        if (size == 0) {
            return null;
        }
        size--;
        DoubleNode head = new DoubleNode((int) (Math.random() * (maxValue + 1)));
        DoubleNode pre = head;
        while (size > 0) {
            size--;
            DoubleNode cur = new DoubleNode((int) (Math.random() * (maxValue + 1)));
            pre.setNext(cur);
            cur.setLast(pre);
            pre = cur;
        }
        return head;
    }

    public static void printLinkedList(INode node) {
        while (node != null) {
            System.out.print(node.getValue() + "");
            if (node.getNext() != null) {
                System.out.print(",");
            }
            node = node.getNext();
        }
        System.out.println();
    }
}
